package com.mvp.shq.washer.ui.wallet;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.BillAdapter;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.entity.Bill;
import com.mvp.shq.washer.entity.User;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.rxbus.Subscribe;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.JumpUtil;
import com.mvp.shq.washer.utils.RecyclerViewDivider;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.T;
import com.mvp.shq.washer.view.other.AppBarStateChangeListener;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WalletActivity extends BaseActivity<WalletPresenter, WalletModel> implements WalletConstract.View {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.btn_recharge)
    Button btnRecharge;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.appbar_layout)
    AppBarLayout appBarLayout;
    @BindView(R.id.tv_current_money)
    TextView tvCurrentMoney;
    private User user;
    private int pageNo = 1;
    private int pageSize = 10;
    private boolean isLoadMore = false;
    private List<Bill> bills;
    private BillAdapter adapter;
    @Override
    public int getLayoutId() {
        return R.layout.activity_wallet;
    }

    @Override
    public void initView() {
        title.setText("我的钱包");
        title.setVisibility(View.GONE);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        RecyclerViewDivider divider = new RecyclerViewDivider(context, LinearLayoutManager.VERTICAL, 30, R.color.white);
        //recyclerView.addItemDecoration(divider);
        bills = new ArrayList<>();
        adapter = new BillAdapter(context,bills);
        recyclerView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                isLoadMore = false;
                refreshLayout.setEnableLoadmore(true);
                pageNo = 1;
                presenter.getBills(pageNo,pageSize);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                isLoadMore = true;
                pageNo ++;
                presenter.getBills(pageNo,pageSize);
            }
        });
        appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                if( state == State.EXPANDED ) {

                    //展开状态
                    title.setVisibility(View.GONE);

                }else if(state == State.COLLAPSED){

                    //折叠状态
                    title.setVisibility(View.VISIBLE);

                }else {

                    //中间状态
                    title.setVisibility(View.GONE);

                }
            }
        });
        refreshLayout.autoRefresh();
        setMoney();
    }
    @Subscribe(code = EventCode.UPDATE_USER)
    public void setMoney(){
        user = SpUtil.getUser();
        tvBalance.setText(user.getRechargeBalance().add(user.getGivenMoney())+"");
        tvCurrentMoney.setText("余额(含增送金额"+user.getGivenMoney()+"元)");
    }


    @OnClick({R.id.back, R.id.btn_recharge,R.id.btn_withdraw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.btn_recharge:
                //RxBus.get().send(EventCode.TO_RECHARGE);
                this.setResult(66);
                finish();
                break;
            case R.id.btn_withdraw:
                JumpUtil.toWithDraw(context);
                break;
        }
    }

    @Override
    public void getBillsS(List<Bill> list) {
        if (list == null){
            refreshLayout.finishLoadmore();
            refreshLayout.finishRefresh();
            return;
        }
        if (!isLoadMore){
            bills.clear();
            refreshLayout.finishRefresh();
        }else {
            refreshLayout.finishLoadmore();
        }
        if (list.size() < pageSize){
            refreshLayout.setEnableLoadmore(false);
        }
        bills.addAll(list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void getBillsF(String msg) {
        T.show(msg);
        refreshLayout.finishLoadmore();
        refreshLayout.finishRefresh();
    }
}
